import App from './App'

// #ifndef VUE3
import Vue from 'vue'

import wechat from './utils/wx'
import utils from './utils/utils.js'
import http from './utils/http'

import CustomToast from '@/components/CustomToast.vue'
// 创建构造器
const ToastConstructor = Vue.extend(CustomToast)
// 全局挂载
const showToast = (message, duration = 2000) => {
  const instance = new ToastConstructor({
    el: document.createElement('div')
  })
  
  document.body.appendChild(instance.$el)
  instance.show(message, duration)
}
Vue.prototype.$showToast = showToast


Vue.prototype.$wx = wechat
Vue.prototype.$http = http
Vue.prototype.$utils = utils


import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif

// #ifdef H5
 
// import  VConsole  from  'vconsole';
// new VConsole();
 
// #endif


Vue.mixin({
    methods: {
        setData: function(obj, callback) {
            let that = this;
            const handleData = (tepData, tepKey, afterKey) => {
                tepKey = tepKey.split('.');
                tepKey.forEach(item => {
                    if (tepData[item] === null || tepData[item] === undefined) {
                        let reg = /^[0-9]+$/;
                        tepData[item] = reg.test(afterKey) ? [] : {};
                        tepData = tepData[item];
                    } else {
                        tepData = tepData[item];
                    }
                });
                return tepData;
            };
            const isFn = function(value) {
                return typeof value == 'function' || false;
            };
            Object.keys(obj).forEach(function(key) {
                let val = obj[key];
                key = key.replace(/\]/g, '').replace(/\[/g, '.');
                let front, after;
                let index_after = key.lastIndexOf('.');
                if (index_after != -1) {
                    after = key.slice(index_after + 1);
                    front = handleData(that, key.slice(0, index_after), after);
                } else {
                    after = key;
                    front = that;
                }
                if (front.$data && front.$data[after] === undefined) {
                    Object.defineProperty(front, after, {
                        get() {
                            return front.$data[after];
                        },
                        set(newValue) {
                            front.$data[after] = newValue;
                            that.$forceUpdate();
                        },
                        enumerable: true,
                        configurable: true
                    });
                    front[after] = val;
                } else {
                    that.$set(front, after, val);
                }
            });
            // this.$forceUpdate();
            isFn(callback) && this.$nextTick(callback);
        }
    }
});
